#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QMenu>
#include <QAction>
#include <QToolBar>
#include <QFileDialog>
#include "document/omdocument.h"

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    createActions();
    createMenus();
    createToolBars();
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::setDocument(OMDocument* document)
{
    mDocument = document;
}

void MainWindow::setOperationManager(OMOperationManager* operationManager)
{
    mOperationManager = operationManager;
}

void MainWindow::createActions(void)
{
    // Load file
    mLoadFileAction = new QAction(tr("&Load File"), this);
    mLoadFileAction->setIcon(QIcon(":/resources/icons/loadfile.png"));
    mLoadFileAction->setShortcut(QKeySequence::Open);
    mLoadFileAction->setStatusTip(tr("Load a security code file"));
    connect(mLoadFileAction, SIGNAL(triggered()), this, SLOT(loadFile()));

    // Exit
    mExitAction = new QAction(tr("E&xit"), this);
    mExitAction->setIcon(QIcon(":/resources/icons/exit.png"));
    mExitAction->setShortcut(QKeySequence::Close);
    mExitAction->setStatusTip(tr("Exit"));
    connect(mExitAction, SIGNAL(triggered()), this, SLOT(close()));
}

void MainWindow::createMenus(void)
{
    mFileMenu = menuBar()->addMenu(tr("&File"));
    mFileMenu->addAction(mLoadFileAction);
    mFileMenu->addSeparator();
    mFileMenu->addAction(mExitAction);

    mOperationsMenu = menuBar()->addMenu(tr("&Operations"));
}

void MainWindow::createToolBars(void)
{
    mFileToolbar = addToolBar(tr("&File"));
    mFileToolbar->addAction(mLoadFileAction);
    mFileToolbar->addSeparator();
    mFileToolbar->addAction(mExitAction);

    mOperationsToolbar = addToolBar(tr("&Operations"));
}

void MainWindow::loadFile(void)
{
    QString filePath = QFileDialog::getOpenFileName(this, tr("Load file"), "", tr("Image Files (*.png *.jpg *.bmp)"));
    if(!filePath.isEmpty())
    {
       mDocument->loadFile(filePath);
    }
}

QMenu* MainWindow::operationsMenu(void)
{
    return mOperationsMenu;
}

QToolBar* MainWindow::operationsToolBar(void)
{
    return mOperationsToolbar;
}
